package com.akaovi.chatroom.scene;

import com.akaovi.chatroom.client.Client;
import com.akaovi.chatroom.mess.Login;
import com.akaovi.chatroom.mess.MessType;
import com.akaovi.chatroom.mess.ResponseStatus;
import com.akaovi.chatroom.process.OwnThread;
import com.akaovi.chatroom.utils.CommonUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author akaovi
 * @version 1.0
 * @description  登陆界面
 * @date 2022/12/9 0:23
 * @since 2022/12/9 0:23
 */
public class LoginScene {
    static double loginHeight = 300;
    static double loginWidth = 400;
    static VBox vBox = new VBox();
    static Label textLogin = new Label("登录");
    static TextField account = new TextField();  // 存在用户 7777， 8888 密码都为 123456
    static PasswordField pwd = new PasswordField();
    static HBox loginOrRegister = new HBox();
    static Button loginBtn = new Button("登录");
    static Button registerBtn = new Button("注册");
    static Scene loginScene = new Scene(vBox, loginWidth, loginHeight);

    // 加盐
    public static final String SALT = "jnfukrwbnvj";

    static {
        //登录场景的初始化
        vBox.setAlignment(Pos.CENTER);
        vBox.setSpacing(20);
        textLogin.setFont(new Font(20));
        account.setPromptText("请输入账号: ");
        account.setFocusTraversable(false);
        account.setMaxWidth(200);
        pwd.setPromptText("请输入密码: ");
//        pwd.setText("123456");
        pwd.setFocusTraversable(false);
        pwd.setMaxWidth(200);
        loginOrRegister.getChildren().addAll(loginBtn, registerBtn);
        loginOrRegister.setSpacing(20);
        loginOrRegister.setAlignment(Pos.CENTER);
        vBox.getChildren().addAll(textLogin, account, pwd, loginOrRegister);

        // 绑定登录事件
        loginBtn.setOnAction(event -> {
            int account;
            try {
                account = Integer.parseInt(LoginScene.account.getText());
            } catch (Exception e) {
                System.out.println("账号有误!");
                return;
            }
//            // 直接跳过验证，调试
//            if (account == 9527 && "123456".equals(pwd.getText())) {
//                //  转换场景
//                MainScene.changeToMainScene();
//                MainScene.insertTwoFriend();
//                MainScene.insertTwoChat();
//                return;
//            }
            try {
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 20000);
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(new Login(MessType.Login, account, CommonUtils.getMd5(pwd.getText() + SALT)));
                oos.flush();
                // 接收ReLogin 判断是否登录成功
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ResponseStatus mess = (ResponseStatus) ois.readObject();
                if (mess.getStatus() != 200) {
                    System.out.println("登陆失败");
                    ois.close();
                    oos.close();
                    socket.close();
                } else {
                    System.out.println("登陆成功");
                    Client.ot = new OwnThread(socket, ois, oos, account);
                    new Thread(Client.ot).start();
                    //  转换场景
                    MainScene.changeToMainScene();
//                    MainScene.insertTwoChat();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // 绑定注册跳转事件
        registerBtn.setOnAction(e -> {
            RegisterScene.changeToRegisterScene();
        });
    }

    /**
     * 转到登录场景
     */
    public static void changeToLoginScene() {
        Client.getStage().setScene(LoginScene.loginScene);
    }
}
